package com.order.controller.seller;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.order.pojo.OrderDetail;
import com.order.pojo.OrderMaster;
import com.order.service.IOrderDetailService;
import com.order.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "订单详情接口")
@RestController
@RequestMapping("/seller/order-detail")
public class OrderDetailController {
    @Autowired
    IOrderDetailService orderDetailService;

    @ApiOperation(value = "获取所有订单详情信息")
    @GetMapping("/")
    public Result getAllOrderDetail() {
        List<OrderDetail> orderDetails = orderDetailService.list();
        if (orderDetails == null || orderDetails.isEmpty()) {
            return Result.error("没有订单详情信息");
        }
        return Result.success(orderDetails);
    }

    @ApiOperation(value = "根据ID获取订单详情信息")
    @GetMapping("{detailId}")
    public Result getOrderDetailById(@PathVariable("detailId") String detailId) {
        OrderDetail orderDetail = orderDetailService.getById(detailId);
        if (orderDetail == null) {
            return Result.error("订单详情信息不存在");
        }
        return Result.success(orderDetail);
    }

    @ApiOperation(value = "添加订单详情信息")
    @PostMapping("/add")
    public Result addOrderDetail(@RequestBody OrderDetail orderDetail) {
        if(orderDetailService.save(orderDetail)){
            return Result.success("订单详情添加成功");
        } else {
            return Result.error("订单详情添加失败");
        }
    }

    @ApiOperation(value = "更新订单详情信息")
    @PutMapping("/update")
    public Result updateOrderDetail(@RequestBody OrderDetail orderDetail) {
        orderDetail.setUpdateTime(DateUtil.now());
        if(orderDetailService.updateById(orderDetail)){
            return Result.success("订单详情更新成功");
        } else {
            return Result.error("订单详情更新失败");
        }
    }

    @ApiOperation(value = "删除订单详情信息")
    @DeleteMapping("/delete/{detailId}")
    public Result deleteOrderDetail(@PathVariable("detailId") String detailId) {
        OrderDetail orderDetail = new OrderDetail(detailId, 1);
        if(orderDetailService.updateById(orderDetail)){
            return Result.success("订单详情删除成功");
        } else {
            return Result.error("订单详情删除失败");
        }
    }

    @ApiOperation(value = "根据页面获取所有订单详情信息")
    @GetMapping("/selectPage")
    public Result getOrderDetailByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                                           @RequestParam(value = "pageSize", defaultValue = "1000") Integer size,
                                           OrderDetail orderDetail) {
        PageInfo<OrderDetail> pageInfo = orderDetailService.getOrderDetailByPage(page, size, orderDetail);
        return Result.success(pageInfo);
    }

}
